import "babel-polyfill";
import App from './App'
// 导入vue路由
import router from './router'
// 导入store
import store from './vuex/index'
// 导入原生路由插件
import Navigation from 'vue-navigation'
// 导入图片懒加载
import VueLazyload from 'vue-lazyload'
// vConsole
// import Vconsole from 'vconsole'
// let vConsole = new Vconsole()
// export default vConsole
import axios from 'axios';

import VueCountdown from '@chenfengyuan/vue-countdown';
Vue.component(VueCountdown.name, VueCountdown)

Vue.use(VueRate)
// 导入复制粘贴
import VueClipboard from 'vue-clipboard2'
Vue.use(VueClipboard)

Vue.use(Navigation, { router, store })
Vue.use(VueLazyload)
// 打印
Vue.prototype.print = (obj, type) => {
  type = type || "log";
  const log = JSON.parse(JSON.stringify(obj));
  console[type](log)
}
import globalConfig from './config'

// 公司存储的唯一标识
Vue.prototype.companyUnique  = globalConfig.companyUnique

// 引入UI组件
import MintUI from 'mint-ui'
import 'mint-ui/lib/style.css'
Vue.use(MintUI)

// 引入ajax
import Ajax from '@/fetch/ajax.js'
Vue.use(Ajax)
// 公共方法
import utilCommon from './util/common'
Vue.use(utilCommon);



import userInfo from './util/userInfo'

// 测试用


router.beforeEach((to, from, next) => {
let user = window.localStorage.getItem(globalConfig.companyUnique)
if(!user || user == undefined || user == 'undefined') {
  window.localStorage.removeItem(globalConfig.companyUnique)
}
  // 本地测试用
  if(utilCommon.ENV()){
    window.localStorage.setItem(globalConfig.companyUnique, JSON.stringify(userInfo.userInfo))
  }
  next()
})

Vue.config.productionTip = false

router.afterEach((to, from, next) => {
  // 获取当前用户时间戳 如果不存在的,设置今天的时间戳 如果存在并对比时间戳,是否是今天
  let userInfoTimesTamp = window.localStorage.getItem('userInfoTimesTamp')
  userInfoTimesTamp ? globalConfig.closeTimesTRamp(parseInt(userInfoTimesTamp)) : globalConfig.setlocalStorage('userInfoTimesTamp', new Date().getTime())
  if (!userInfoTimesTamp) {
    window.localStorage.removeItem(Vue.prototype.companyUnique)
  }
  window.scrollTo(0, 0)
  // 如果当前路由是MyMoney 并且没有绑定手机去绑定手机
  if (to.name == 'MyMoney') {
    let ethelWxUserInfo = globalConfig.getlocalStorage(Vue.prototype.companyUnique);
    if (!ethelWxUserInfo.is_bind_phone) {
      return router.push({
        path: "/entry/register"
      })
    }
  }

  // 获取用户信息
  var ethelWxUserInfo = globalConfig.getlocalStorage(Vue.prototype.companyUnique);
  // 获取code参数
  var code = globalConfig.getUrlParams('code');
  // 如果当前用户信息不存在或者等于undefined就删除用户信息
  if (ethelWxUserInfo == 'undefined' || !ethelWxUserInfo || !ethelWxUserInfo.id) {
    window.localStorage.removeItem(Vue.prototype.companyUnique)
  }

  // 如果code存在调用微信的接口 cod
  if ((code && !ethelWxUserInfo)  || (code && ethelWxUserInfo == 'undefined')) {
    globalConfig.fnHandleWxLogin(code, globalConfig.getUrlParams('master_openid'))
  } else {
    // 没有微信用户信息，没有授权-->> 需要授权，跳转授权页面
    if (globalConfig.browerInfo.match(/MicroMessenger/i) == 'micromessenger' && !ethelWxUserInfo && !ethelWxUserInfo.id) {
      if(to.query.openid && to.query.id) {
        // 调用接口
        globalConfig.fnGetWxUserInfo(to.query.openid, to.query.id)
        return
      }
      var goUrl = '';
      if(window.location.href.indexOf("?") != -1 && !globalConfig.getUrlParams('encode')){
        goUrl = window.location.protocol+'//'+window.location.host+window.location.pathname + '?encode=' + encodeURIComponent(window.location.href.split('?')[1])
      }else{
        goUrl = window.location.protocol+'//'+window.location.host+window.location.pathname+'?'+decodeURIComponent(globalConfig.getUrlParams('encode'))
      }
      window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + globalConfig.appid + '&redirect_uri=' + goUrl + '&response_type=code&scope=snsapi_userinfo#wechat_redirect';
    }

  }


});


/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  template: '<App/>',
  components: { App }
})
